﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Tfs_analyzer;
using System.Diagnostics;
using Microsoft.TeamFoundation.Server;
using Tfs_analyzer.Utils;


namespace TfsApplication
{
	class Program
	{
		static void Main(String[] args)
		{
    if (args.Length < 1)
      {
      Console.WriteLine("Please, specify the path for configuration file as input parameter.");
      return;
      }
  
    IniParser parser = new IniParser(@args[0]);

    Dictionary<string, List<string>> project_team_list = new Dictionary<string, List<string>>();
    List<string> teams_a = new List<string>();
    var groups_a = parser.GetSetting("TeamsInfo", "user_groups_a").Split(';');
    foreach (var group in groups_a)
      teams_a.Add(group.Trim());
   
    project_team_list.Add(parser.GetSetting("TeamsInfo", "tfs_path_a"), teams_a);

    List<string> teams_b = new List<string>();
    var groups_b = parser.GetSetting("TeamsInfo", "user_groups_b").Split(';');
    foreach (var group in groups_b)
      teams_b.Add(group.Trim());

    project_team_list.Add(parser.GetSetting("TeamsInfo", "tfs_path_b"), teams_b);

    int days = Convert.ToInt32(parser.GetSetting("AppConfig", "period"));
    string path = parser.GetSetting("AppConfig", "report_path") + parser.GetSetting("AppConfig", "report_name");


    TfsConfigurationServer tfs = ConnectToServer();

    ReadOnlyCollection<CatalogNode> collectionNodes = tfs.CatalogNode.QueryChildren(new[] { CatalogResourceTypes.ProjectCollection },	false, CatalogQueryOptions.None);

	  foreach (CatalogNode collectionNode in collectionNodes)
			{
      var serv = GetVersionControlServer(collectionNode, tfs);
     
      ChangesetReportGenerator report = new ChangesetReportGenerator();
      Collection<ValidationSummary> validation = new Collection<ValidationSummary>();
      

      foreach (var project in project_team_list)
        {
        validation.Add(report.GenerateReportFor(tfs, collectionNode, project.Key, project.Value, TimeSpan.FromDays(days)));
        }
      
      ValidationXLSWriter.ExportToExcel(validation, path, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook);
			}
		}

    static TfsConfigurationServer ConnectToServer()
      {
      string tfsName = @"https://tfs.materialise.net:443/tfs";
      Trace.WriteLine("Connecting to TSF server: " + tfsName);
      TfsConfigurationServer tfs = TfsConfigurationServerFactory.GetConfigurationServer(new Uri(tfsName), new UICredentialsProvider());
      tfs.EnsureAuthenticated();
      Trace.WriteLine("Connected successfully");
      return tfs;
      }
  
    static VersionControlServer GetVersionControlServer(CatalogNode collectionNode, TfsConfigurationServer tfs)
      {
      // Use the InstanceId property to get the team project collection
      Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
      TfsTeamProjectCollection teamProjectCollection = tfs.GetTeamProjectCollection(collectionId);
      var serv = teamProjectCollection.GetService<VersionControlServer>();
      return serv;
      }

	}
}